package com.ruoyi.blindBoxManage.service.impl;

import com.ruoyi.blindBoxManage.domain.ProductType;
import com.ruoyi.blindBoxManage.mapper.BlindBoxProductMapper;
import com.ruoyi.blindBoxManage.mapper.ProductTypeMapper;
import com.ruoyi.blindBoxManage.service.IProductTypeService;
import com.ruoyi.common.utils.DateUtils;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 商品等级配置Service业务层处理
 *
 * @author ruoyi
 * @date 2025-08-12
 */
@Service
public class ProductTypeServiceImpl implements IProductTypeService {
    @Autowired
    private ProductTypeMapper productTypeMapper;
    @Resource
    private BlindBoxProductMapper blindBoxProductMapper;

    /**
     * 查询商品等级配置
     *
     * @param id 商品等级配置主键
     * @return 商品等级配置
     */
    @Override
    public ProductType selectProductTypeById(Integer id) {
        return productTypeMapper.selectProductTypeById(id);
    }

    /**
     * 查询商品等级配置列表
     *
     * @param productType 商品等级配置
     * @return 商品等级配置
     */
    @Override
    public List<ProductType> selectProductTypeList(ProductType productType) {
        return productTypeMapper.selectProductTypeList(productType);
    }

    /**
     * 新增商品等级配置
     *
     * @param productType 商品等级配置
     * @return 结果
     */
    @Override
    public int insertProductType(ProductType productType) {
        productType.setCreateTime(DateUtils.getNowDate());
        return productTypeMapper.insertProductType(productType);
    }

    /**
     * 修改商品等级配置
     *
     * @param productType 商品等级配置
     * @return 结果
     */
    @Override
    public int updateProductType(ProductType productType) {
        productType.setUpdateTime(DateUtils.getNowDate());
        return productTypeMapper.updateProductType(productType);
    }

    /**
     * 批量删除商品等级配置
     *
     * @param ids 需要删除的商品等级配置主键
     * @return 结果
     */
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public int deleteProductTypeByIds(Integer[] ids) {
        for (Integer id : ids) {
            Integer count = blindBoxProductMapper.selectCountByProductTypeId(id);
            if (count > 0) {
                throw new RuntimeException("id为:" + id + " 有商品绑定");
            }
        }
        return productTypeMapper.deleteProductTypeByIds(ids);
    }

    /**
     * 删除商品等级配置信息
     *
     * @param id 商品等级配置主键
     * @return 结果
     */
    @Override
    public int deleteProductTypeById(Integer id) {
        return productTypeMapper.deleteProductTypeById(id);
    }
}
